<template>
	<el-container>
		<el-container>
			<el-header>
						<div class="left-panel">
							<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
						</div>
						<div class="right-panel">
							<el-select style="width: 100px;" v-model="search.act_type" clearable
								placeholder="类型" filterable>
								<el-option v-for="(item, key) in act_types" :key="key" :label="item" :value="key" />
							</el-select>
							<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						</div>
					</el-header>
					<el-main class="nopadding">
						<scTable ref="table" :apiObj="list.apiObj" row-key="id" stripe>
							<el-table-column label="ID" prop="id" width="50"></el-table-column>
							<el-table-column label="活动名字" prop="act_name" width="100">
							</el-table-column>
							<el-table-column label="类型" prop="act_type" width="80">
								<template #default="scope">
									<p >{{act_types[scope.row.act_type]}}</p>
								</template>
							</el-table-column>
							<!-- <el-table-column label="活动规则" prop="rules" width="100">
							</el-table-column> -->
							<el-table-column label="活动封面" prop="cover" width="100">
												<template #default="scope">
													<el-image :preview-src-list="[IMG_URL+scope.row.cover]"
							:initial-index="0" style="max-height: 100px;" fit="contain" :src="IMG_URL+scope.row.cover" mode=""></el-image>
												</template>
											</el-table-column>
							<el-table-column label="活动时间" prop="send_time" width="200">
								<template #default="scope">
									<p >开始：{{$TOOL.dateFormat(scope.row.start_time*1000)}}</p>
									<p >结束：{{$TOOL.dateFormat(scope.row.end_time*1000)}}</p>
								</template>
							</el-table-column>
							<el-table-column label="操作" fixed="right" align="right" width="100">
								<template #default="scope">
									<el-button type="primary" plain size="small" @click="table_edit(scope.row)">编辑</el-button>
									<!-- <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="primary" size="small">删除</el-button>
										</template>
									</el-popconfirm> -->
								</template>
							</el-table-column>
						</scTable>
					</el-main>
		</el-container>

	</el-container>

	<!-- <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog> -->


</template>

<script>
	import saveDialog from './components/activitySave.vue'
import configIndex from "@/config"
	export default {
		name: 'configureList',
		components: {
			saveDialog,
		},
		data() {
			return {
				dialog:{
					save: false,
				},
				search: {
					act_type: '',
					// receive_type: '',
					// use_type: '',
					// status: '',
				},
				list: {
					apiObj: this.$API.marketing.activity.list
				},
				act_types: {'1': '大转盘', '2': '砸金蛋', '3': '刮刮乐'},
				IMG_URL: configIndex.IMG_URL
			}
		},
		mounted() {

		},
		methods: {
			//删除
			async table_del(row){
				var reqData = {id: row.id}
				var res = await this.$API.marketing.activity.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.msg, "提示", {type: 'error'})
				}
			},
			//搜索
			upsearch() {
				// console.log(112, this.search)
				let search = JSON.parse(JSON.stringify(this.search))
				this.$refs.table.upData({
					...search
				})
			},
			add(){
				// this.dialog.save = true
				// this.$nextTick(() => {
				// 	this.$refs.saveDialog.open()
				// })
				this.$router.push({
					path: '/marketing/activitySave/0'
				})
			},
			//窗口编辑
			table_edit(row){
				// this.dialog.save = true
				// this.$nextTick(() => {
				// 	this.$refs.saveDialog.open('edit').setData(row)
				// })
				this.$router.push({
					path: '/marketing/activitySave/'+row.id,
				})
			},

			//本地更新数据
			handleSaveSuccess(data, mode){
				//为了减少网络请求，直接变更表格内存数据
				this.dialog.save=false
				if(mode=='add'){
					this.$refs.table.unshiftRow(data)
				}else if(mode=='edit'){
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			},
			open(url) {
				window.open(url, '_blank')
			}
		}
	}
</script>

<style>
</style>
